#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDialog>
#include <QPushButton>
#include <dialog.h>
#include <dialog_ui.h>
#include <QMessageBox>
#include <QColorDialog>
#include <QDebug>
#include <QFileDialog>
#include <QFontDialog>
#include <QInputDialog>



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPushButton* button = new QPushButton(this);
    button->resize(200,40);
    button->setText("打开一个非模态对话框");
    button->move(200,200);
    connect(button,&QPushButton::clicked,this,[=](){
         // 要定义在堆上面，定义在栈上，当函数调用完后，窗口就会销毁
         // 直接一闪而过
//       QDialog dialog(this);
//       dialog.setWindowTitle("非模态窗口");
//       dialog.resize(100,100);
//       dialog.show();
         QDialog* dialog = new QDialog(this);
         dialog->setWindowTitle("非模态对话框");
         dialog->resize(400,300);
         dialog->show();
         // 如果直接delete的话，窗口会一闪而过。
         // 如果不是放又会造成内存泄漏
//         delete dialog;
         // 需要设置特定的属性
         dialog->setAttribute(Qt::WA_DeleteOnClose);


    });


    QPushButton* button1 = new QPushButton(this);
    button1->resize(200,40);
    button1->setText("打开一个非模态对话框");
    button1->move(200,400);
    connect(button1,&QPushButton::clicked,this,[=](){
         QDialog* dialog = new QDialog(this);
         dialog->setWindowTitle("非模态对话框");
         dialog->resize(400,300);
         dialog->show();
         dialog->setAttribute(Qt::WA_DeleteOnClose);


    });

    // UI界面 Action新键，点击新键的槽函数
    connect(ui->action,&QAction::triggered,this,[=]()
    {
//       QDialog dialog(this);
//       dialog.setWindowTitle("模态窗口");
//       dialog.resize(100,100);
//       dialog.exec();
        QDialog* dialog = new QDialog(this);
        dialog->setWindowTitle("模态对话框");
        dialog->resize(400,300);
        dialog->exec();
        // 如果直接delete的话，窗口会一闪而过。
        delete dialog;
    });

    // 自定义对话框 - 代码
    QPushButton* button2 = new QPushButton(this);
    button2->resize(200,40);
    button2->setText("打开一个自定义对话框-code");
    button2->move(200,60);
    connect(button2,&QPushButton::clicked,this,[=](){
         Dialog* dialog = new Dialog(this);
         dialog->setWindowTitle("自定义对话框");
         dialog->resize(400,300);
         dialog->show();
         dialog->setAttribute(Qt::WA_DeleteOnClose);


    });

    // 自定义对话框 - ui
    QPushButton* button3 = new QPushButton(this);
    button3->resize(200,40);
    button3->setText("打开一个自定义对话框-ui");
    button3->move(200,120);

    Dialog_ui* dialog = new Dialog_ui(this);
    dialog->setWindowTitle("自定义对话框-ui");
    dialog->resize(400,300);
    dialog->setAttribute(Qt::WA_DeleteOnClose);
    connect(button3,&QPushButton::clicked,this,[=](){
         dialog->show();
    });


    // 打开消息对话框
    QPushButton* button4 = new QPushButton(this);
    button4->resize(200,40);
    button4->setText("打开消息对话框");
    button4->move(500,120);
    // 创建消息对话框
    QMessageBox* msg = new QMessageBox(this);
    // 设置消息对话框的窗口标题
    msg->setWindowTitle("Warning Message");
    // 设置消息对话框的内容
    msg->setText("警告");
    // 设置消息对话框的类型 - 图标
    msg->setIcon(QMessageBox::Question);
    // 设置消息对话框上的按钮
    msg->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    connect(button4,&QPushButton::clicked,this,[=](){
        msg->show();
    });

    // 打开颜色对话框

    QPushButton* button5 = new QPushButton(this);
    button5->resize(200,40);
    button5->setText("打开颜色对话框");
    button5->move(500,160);
    // 创建颜色对话框
    QColorDialog* cdlg = new QColorDialog(this);

    connect(button5,&QPushButton::clicked,this,[=](){
        // 从颜色对话框中获取颜色
        QColor color = cdlg->getColor();
        qDebug() << "r = " << color.red();
        qDebug() << "g = " << color.green();
        qDebug() << "b = " << color.blue();

    });

    // 打开文件对话框
    QPushButton* button6 = new QPushButton(this);
    button6->resize(200,40);
    button6->setText("打开文件对话框");
    button6->move(500,200);
    // 创建文件对话框
    QFileDialog* fdlg = new QFileDialog(this);
    connect(button6,&QPushButton::clicked,this,[=](){
        // 父对象，窗口标题，打开文件的路径
        QString str = fdlg->getOpenFileName(this,"打开文件","C:/Code/algorithm");
        qDebug() << str;
    });

    QPushButton* button7 = new QPushButton(this);
    button7->resize(200,40);
    button7->setText("打开文件对话框-保存");
    button7->move(500,240);
    connect(button6,&QPushButton::clicked,this,[=](){
        // 父对象，窗口标题，保存的路径
        QString str = fdlg->getSaveFileName(this,"保存文件","C:\\Users\\29358\\Desktop");
        qDebug() << str;
    });

    // 字体对话框
    QPushButton* button8 = new QPushButton(this);
    button8->resize(200,40);
    button8->setText("打开字体对话框");
    button8->move(500,240);

    connect(button8,&QPushButton::clicked,this,[=](){
        bool flag;
        // QFont font = QFontDialog::getFont(&flag);
        // 可以通过参数设置默认的字体
        QFont font = QFontDialog::getFont(&flag,QFont("宋体",20));
        button8->setFont(font);
        qDebug() << "flag = " << flag;
        qDebug() << "字体" <<font.family();
        qDebug() << "字号 "<< font.pointSize();
        qDebug() << "是否加粗" << font.bold();
        qDebug() << "是否倾斜" << font.italic();
    });


    // 输入对话框
    QPushButton* button9 = new QPushButton(this);
    button9->resize(200,40);
    button9->setText("打开双精度浮点型输入对话框");
    button9->move(500,280);
    QInputDialog* idlg = new QInputDialog();
    connect(button9,&QPushButton::clicked,this,[=](){
        double input = idlg->getDouble(this,"浮点型输入框","浮点数");
        qDebug() << input;
    });

    QPushButton* button10 = new QPushButton(this);
    button10->resize(200,40);
    button10->setText("打开双精度浮点型输入对话框");
    button10->move(500,320);

    connect(button10,&QPushButton::clicked,this,[=](){
        int input = idlg->getInt(this,"整型输入框","正数");
        qDebug() << input;
    });

    QPushButton* button11 = new QPushButton(this);
    button11->resize(200,40);
    button11->setText("打开双精度浮点型输入对话框");
    button11->move(500,360);

    connect(button11,&QPushButton::clicked,this,[=](){
        QStringList items;
        items.push_back("香蕉");
        items.push_back("西瓜");
        items.push_back("哈密瓜");
        QString input = idlg->getItem(this,"条目输入框","条目",items);
        qDebug() << input;
    });


}

MainWindow::~MainWindow()
{
    delete ui;
}

